package com.tsd.jxc.stock.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.common.controller.BaseController;
import com.tsd.core.constants.Constants;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.setting.JxcSettingUtil;
import com.tsd.jxc.stock.entity.JxcStockGoods;
import com.tsd.jxc.stock.entity.JxcStockGoodsExt;
import com.tsd.jxc.stock.job.JxcStockJob;
import com.tsd.jxc.stock.service.JxcStockGoodsService;
import com.tsd.jxc.stock.vo.JxcStockGoodsVo;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserExt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：JxcStockGoods商品库存表 控制层
 *
 * @author Hillpool
 * @date 2022/07/29 15:06:33
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "进销存-商品库存表", description = "商品库存表的API描述")
public class JxcStockGoodsController extends BaseController {

    @Resource
    private JxcStockGoodsService jxcStockGoodsService;
    @Resource
    private JxcStockJob jxcStockJob;

    @ApiOperation(value = "根据sid获取商品库存表", notes = "根据sid获取商品库存表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "商品库存表sid", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getJxcStockGoodsBySid.action", method = RequestMethod.POST)
    public HttpResult<JxcStockGoods> getJxcStockGoodsBySid() {
        HttpResult<JxcStockGoods> hr = new HttpResult<JxcStockGoods>();
        try {
            Map<String, Object> map = super.getParams(request);
            String sid = HlpUtils.getString(map, "sid");
            super.checkEmpty(sid, "请提供参数sid");
            JxcStockGoods jxcStockGoods = jxcStockGoodsService.queryBySid(sid);
            hr.setData(jxcStockGoods);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除商品库存表", notes = "根据id删除商品库存表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sids", value = "商品库存表sids", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteJxcStockGoodss.action", method = RequestMethod.POST)
    public HttpResult<String> deleteJxcStockGoodss() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object sids = map.get("sids");
            super.checkEmpty(sids, "请提供参数sids");
            List<String> list = JSON.parseArray(JSON.toJSONString(sids), String.class);
            jxcStockGoodsService.deleteJxcStockGoodss(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存商品库存表", notes = "保存商品库存表，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "商品库存表", required = true, paramType = "body", dataType = "JxcStockGoods")
    })
    @ResponseBody
    @RequestMapping(value = "/saveJxcStockGoods.action", method = RequestMethod.POST)
    public HttpResult<Object> saveJxcStockGoods() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map);
            JxcStockGoods t = JSON.parseObject(json, JxcStockGoods.class);
            jxcStockGoodsService.saveJxcStockGoods(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询商品库存表", notes = "分页查询商品库存表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findJxcStockGoodss.action", method = RequestMethod.POST)
    public HttpResult<Object> findJxcStockGoodss() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "goods_code", "goods_code", false, false);
            HlpUtils.copyStringValue(map, params, "goods_name", "goods_name", false, false);
            HlpUtils.copyStringValue(map, params, "category_sid", "category_sid", false, false);
            HlpUtils.copyStringValue(map, params, "category_full_code", "category_full_code", false, true, true);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            HlpUtils.copyListValue(map, params, "product_list", "product_list", false, String.class);
            PageBean page = new PageBean(map);
            if (map.containsKey(Constants.KEY_NO_PAGE)) {
                page = null;
            }
            if (JxcSettingUtil.isClothing()) {
                List<JxcStockGoodsVo> list = jxcStockGoodsService.findJxcStockGoods4Clothing(params, page);
                hr.setData(list);
            } else {
                List<JxcStockGoodsExt> list = jxcStockGoodsService.findJxcStockGoodss(params, page);
                if (map.containsKey("template")) {
                    List<JxcStockGoodsVo> voList = ListUtil.map(list, JxcStockGoodsExt::convert2GoodsVo);
                    hr.setData(voList);
                } else {
                    hr.setData(list);
                }
            }
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据商品sid获取库存信息包括商品名称等信息", notes = "根据商品sid获取库存信息包括商品名称等信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goods_sid", value = "商品sid", required = false, dataType = "string", paramType = "form"),
    })
    @ResponseBody
    @RequestMapping(value = "/getJxcStockGoodsListByGoodsSid.action", method = RequestMethod.POST)
    public HttpResult<Object> getJxcStockGoodsListByGoodsSid() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            String goods_sid = HlpUtils.getString(map, "goods_sid");
            List<JxcStockGoodsExt> list = jxcStockGoodsService.getJxcStockGoodsListByGoodsSid(goods_sid);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "批量导入商品库存", notes = "批量导入商品库存，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "商品库存表", required = true, paramType = "body", dataType = "JxcStockGoods")
    })
    @ResponseBody
    @RequestMapping(value = "/batchImportStockGoods.action", method = RequestMethod.POST)
    public HttpResult<Object> batchImportStockGoods() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            List<JxcStockGoodsVo> list = super.getParamsList("list", JxcStockGoodsVo.class, true);
            jxcStockGoodsService.batchImportStockGoods(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据sid更改商品库存表", notes = "根据sid更改商品库存表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sids", value = "商品库存表sids", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/updateJxcStockGoodss.action", method = RequestMethod.POST)
    public HttpResult<String> updateJxcStockGoodss() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object sids = map.get("data");
            List<Map> list = JSON.parseArray(JSON.toJSONString(sids), Map.class);
            jxcStockGoodsService.updateJxcStockGoodss(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "根据goods_sid商品货品库存数量", notes = "根据goods_sid商品货品库存数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goods_sid", value = "商品sid", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/startProcJxcStockQue.action", method = RequestMethod.POST)
    public HttpResult<Integer> startProcJxcStockQue() {
        HttpResult<Integer> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            jxcStockJob.startProcJxcStockQue(null);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
